<?php
/**
 *
 * 版权所有：安德玛
 * 作    者：1554324405@qq.com
 * 版本控制: 20210203---新增新会员免费发布任务置顶
 * 功能说明：网红任务控制器。
 *
 **/
namespace Api\Controller;

class OnlineHotController extends ComController
{
    
    /**
     * 媒体分类
     */
    public function mediaCategory()
    {
        $list = M('category')->order('o asc')->field('id,title')->select();
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
    
    /**
     * 任务要求
     */
    public function requirementList()
    {
        $list = M('money_type')->field('id,title,price')->select();
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
    
    /**
     * 发布任务平台收取服务费比列
     */
    public function taskPercent()
    {
		$k = "PUSH_TASK_PERCENT";
		$var = M('setting')->where("k='$k'")->find();
        $percent = $var['v'];
        $this->res['data'] = $percent;
        $this->response($this->res,'json');
    }
    
    /**
     * 会员等级列表
     */
    public function userLevel()
    {
        $list = M('user_level')->where('is_r=1')->order('o asc')->field('id,title')->select();
        $this->res['data'] = $list;
        $this->response($this->res,'json');
    }
    
    /**
     * 发布任务保存数据
     */
    public function save()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请使用POST请求接口';
            $this->response($this->res,'json');
        }
        // 判断当前用户是否满足发布任务条件
        if(strtotime($this->user['expri_day']) < time()){
            $this->res['code'] = 102;
            $this->res['msg'] = '该账户已过期，无发布任务权限！';
            $this->response($this->res,'json');
        }
        $user_level = M('user_level')->where('id='.$this->user['level_id'])->find();
        // if($user_level['is_red'] == 0){
        //     $this->res['code'] = 103;
        //     $this->res['msg'] = '该账户未开通网红权限，无法发布任务！';
        //     $this->response($this->res,'json');
        // }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $data['title'] = $json->title;
        $data['cid'] = $json->cid;
        $data['price'] = $json->price;
        $data['num'] = $json->num;
        $data['url'] = $json->url;
        $data['end_t'] = $json->end_t;
        $data['user_level'] = $json->user_level;
        $data['money_type'] = implode(',', $json->money_type);
        $data['amount_price'] = $json->amount_price;
        $data['service_price'] = $json->service_price;
        $data['pay_id'] = $json->pay_id;
        $data['remark'] = $json->remark;
        if(!empty($json->pic)){
            $data['pic'] = implode('|', $json->pic);
        }
        if(!$data['title'] or !$data['cid'] or !$data['price'] or !$data['num'] or !$data['url']
            or !$data['end_t'] or !$data['user_level'] or !$data['money_type'] or !$data['amount_price'] or !$data['service_price'] or !$data['pay_id']){
                $this->res['code'] = 104;
                $this->res['msg'] = '表单必填项存在空项，请检查表单输入！';
                $this->response($this->res,'json');
        }
        
        if(!$this->checkUrl($data['url'])){
            $this->res['code'] = 105;
            $this->res['msg'] = '任务地址必须以http(s)开头！';
            $this->response($this->res,'json');
        }
        
        $data['task_no'] = getOrderNo();
        $data['t'] = time();
        $data['uid'] = $this->user['id'];
        $data['amount_price'] = floatval($data['amount_price']) + floatval($data['service_price']);
        $res = M('task')->data($data)->add();
        if($res){
            $this->res['msg'] = '任务发布成功，请尽快完成支付！';
            $this->res['data'] = $res;
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = 500;
            $this->res['msg'] = '操作失败，请稍后再试！';
            $this->response($this->res,'json');
        }
    }
    
    
    private function checkUrl($url)
    {
        if(preg_match("/^(http:\/\/|https:\/\/).*$/",$url)){
            return 1;
        }else{
            return 0;
        }
    } 
    
    /**
     * 付款方式列表
     */
    public function payType()
    {
        $pay_type_list = M('pay_type')->order('o asc')->field('id,name,icon')->select();
        foreach ($pay_type_list as $k=>&$v){
            $pay_type_list[$k]['icon'] = C('URL').$v['icon'];
        }
        $this->res['data'] = $pay_type_list;
        $this->response($this->res,'json');
    }
    
    /**
     * 任务详情
     */
    public function taskDetail()
    {
        $id = I('get.id');
        if(!$id){
            $this->res['code'] = 101;
            $this->res['msg'] = '任务id为空！';
            $this->response($this->res,'json');
        }
        // 判断当前用户是否满足发布任务条件
        if(strtotime($this->user['expri_day']) < time()){
            $this->res['code'] = 102;
            $this->res['msg'] = '该账户已过期，无发布任务权限！';
            $this->response($this->res,'json');
        }
        $user_level = M('user_level')->where('id='.$this->user['level_id'])->find();
        // if($user_level['is_red'] == 0){
        //     $this->res['code'] = 103;
        //     $this->res['msg'] = '该账户未开通网红权限，无法发布任务！';
        //     $this->response($this->res,'json');
        // }
        $vo = M('task')->where('id='.$id)->find();
        // 允许接单会员等级
        $user_level = $vo['user_level'];
        $user_level_arr = explode(',', $user_level);
        $where1['id'] = array('in',$user_level_arr);
        $info = M('user_level')->where($where1)->field('title')->select();
        foreach ($info as $val) {
            $val = join(",",$val);
            $temp_array[] = $val;
        }
        $vo['level_info'] = implode(",", $temp_array);
        // 任务要求
        $money_type = $vo['money_type'];
        $money_type_arr = explode(',', $money_type);
        $where2['id'] = array('in',$money_type_arr);
        $money_info = M('money_type')->where($where2)->field('title')->select();
        foreach ($money_info as $val) {
            $val = join(",",$val);
            $temp_array1[] = $val;
        }
        $vo['money_info'] = implode(",", $temp_array1);
        if($vo['pz']){
            $pz_arr = explode(',', $vo['pz']);
            foreach ($pz_arr as $k=>&$v){
                $pz_arr[$k] = C('URL').$v;
            }
            $vo['pz'] = $pz_arr;
        }
        // 已完成数量需要审核有效的接单
        $has_done_num = M('user_task')->where(array('task_id'=>$id,'status'=>2))->count();
        $vo['hasdid_num'] = $has_done_num;
        // 支付方式
        $pay_type = M('pay_type')->where(array('id'=>$vo['pay_id']))->field('name,type,account,qrcode')->find();
        $vo['pay_name'] = $pay_type['name'];
        if($pay_type['type'] == 1){
            $vo['account'] = $pay_type['account'];
        }else{
            $vo['qrcode'] = C('URL').$pay_type['qrcode'];
        }
        $vo['t'] = date('Y-m-d H:i:s',$vo['t']);
        $category = M('category')->where('id='.$vo['cid'])->find();
        $vo['cname'] = $category['title'];
        $this->res['data'] = $vo;
        $this->response($this->res,'json');
        
    }
    
    /**
     * 任务支付申请提交
     */
    public function changeState()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请使用POST请求接口';
            $this->response($this->res,'json');
        }
        // 判断当前用户是否满足发布任务条件
        if(strtotime($this->user['expri_day']) < time()){
            $this->res['code'] = 102;
            $this->res['msg'] = '该账户已过期，无发布任务权限！';
            $this->response($this->res,'json');
        }
        $user_level = M('user_level')->where('id='.$this->user['level_id'])->find();
        // if($user_level['is_red'] == 0){
        //     $this->res['code'] = 103;
        //     $this->res['msg'] = '该账户未开通网红权限，无法发布任务！';
        //     $this->response($this->res,'json');
        // }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $pz = implode(',', $json->pz);
        $id = $json->id;
        if(!$pz or !$id){
            $this->res['code'] = 104;
            $this->res['msg'] = '任务id或支付凭证为空！';
            $this->response($this->res,'json');
        }
        $data = array(
            'pz'=>$pz,
            'update_t'=>time(),
            'pay_status'=>2
        );
        $res = M('task')->data($data)->where('id='.$id)->save();
        if($res){
            $this->res['msg'] = '提交审核成功，请等待管理员审核通过！';
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = 500;
            $this->res['msg'] = '操作失败，请稍后再试！';
            $this->response($this->res,'json');
        }
    }
    
    /**
     * 我的任务列表
     */
    public function myTaskList()
    {
        // 判断当前用户是否满足发布任务条件
        if(strtotime($this->user['expri_day']) < time()){
            $this->res['code'] = 102;
            $this->res['msg'] = '该账户已过期，无发布任务权限！';
            $this->response($this->res,'json');
        }
        $user_level = M('user_level')->where('id='.$this->user['level_id'])->find();
        // if($user_level['is_red'] == 0){
        //     $this->res['code'] = 103;
        //     $this->res['msg'] = '该账户未开通网红权限，无法发布任务！';
        //     $this->response($this->res,'json');
        // }
        
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $model = M();
        $sqlWhere = "a.uid=".$this->user['id']." and ";
        
        $sqlWhere = msubstr($sqlWhere, 0, utf8_strlen($sqlWhere) - 4, "utf-8", false);
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $model->query("select count(*) as total,case when date_format(a.end_t,'%Y-%m-%d') < date_format(now(),'%Y-%m-%d') and a.hasdid_num < a.num
                                then 3 when a.hasdid_num>=a.num then 2 else 1 end as `status` from ".C('DB_PREFIX')."task a left join
                                ".C('DB_PREFIX')."category b on a.cid=b.id where ".$sqlWhere);
        $count = $count[0]['total'];
        $sql = "select a.*,b.title as category_title,b.icon,case when date_format(a.end_t,'%Y-%m-%d') < date_format(now(),'%Y-%m-%d') and a.hasdid_num < a.num
                then 3 when a.hasdid_num>=a.num then 2 else 1 end as `status` from ".C('DB_PREFIX')."task a left join ".C('DB_PREFIX')."category b
                on a.cid=b.id where ".$sqlWhere." order by a.t desc limit ".$offset . "," . $pagesize;
        $list = $model->query($sql);
        foreach ($list as $k=>&$v){
            // 允许接单会员等级
            $user_level = $v['user_level'];
            $user_level_arr = explode(',', $user_level);
            $info_title = '';
            foreach ($user_level_arr as $n=>&$m){
                $info = M('user_level')->where('id='.$m)->field('title')->find();
                $info_title .= $info['title']." ";
            }
            $list[$k]['level_info'] = $info_title;
            
            // 任务要求
            $money_type = $v['money_type'];
            $money_type_arr = explode(',', $money_type);
            $money_info_title = '';
            foreach ($money_type_arr as $h=>&$l){
                $money_info = M('money_type')->where('id='.$l)->field('title')->find();
                $money_info_title .= $money_info['title'].' ';
            }
            $list[$k]['money_info'] = $money_info_title;
            $pz = explode(',', $v['pz']);
            foreach ($pz as $n=>&$pp){
                $pz[$n] = C('URL').$pp;
            }
            $list[$k]['pz'] = $pz;
            $list[$k]['t'] = date('Y-m-d H:i:s',$v['t']);
            $list[$k]['icon'] = C('URL').$v['icon'];
            // 已完成数量需要审核有效的接单
            $has_done_num = M('user_task')->where(array('task_id'=>$v['id'],'status'=>2))->count();
            $list[$k]['hasdid_num'] = $has_done_num;
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
	/**
	 * 发布任务保存数据
	 */
	public function saveFreeFabu()
	{
	    if(!IS_POST){
	        $this->res['code'] = 101;
	        $this->res['msg'] = '请使用POST请求接口';
	        $this->response($this->res,'json');
	    }
	    $post_data = file_get_contents('php://input');
	    $json = json_decode($post_data);
	    $data['title'] = $json->title;
	    $data['cid'] = $json->cid;
	    $data['price'] = $json->price;
	    $data['num'] = $json->num;
	    $data['url'] = $json->url;
	    $data['end_t'] = $json->end_t;
	    $data['user_level'] = $json->user_level;
	    $data['money_type'] = implode(',', $json->money_type);
	    $data['amount_price'] = $json->amount_price;
	    $data['service_price'] = $json->service_price;
	    $data['pay_id'] = $json->pay_id;
	    $data['remark'] = $json->remark;
	    if(!empty($json->pic)){
	        $data['pic'] = implode('|', $json->pic);
	    }
	    if(!$data['title'] or !$data['cid'] or !$data['price'] or !$data['num'] or !$data['url']
	        or !$data['end_t'] or !$data['user_level'] or !$data['money_type'] or !$data['amount_price'] or !$data['service_price'] or !$data['pay_id']){
	            $this->res['code'] = 104;
	            $this->res['msg'] = '表单必填项存在空项，请检查表单输入！';
	            $this->response($this->res,'json');
	    }
	    
	    if(!$this->checkUrl($data['url'])){
	        $this->res['code'] = 105;
	        $this->res['msg'] = '任务地址必须以http(s)开头！';
	        $this->response($this->res,'json');
	    }
	    $data['task_no'] = getOrderNo();
	    $data['t'] = time();
	    $data['uid'] = $this->user['id'];
	    $data['amount_price'] = floatval($data['amount_price']) + floatval($data['service_price']);
		$data['pay_status'] = 2;
		$data['on_top'] = 888;
	    $res = M('task')->data($data)->add();
	    if($res){
	        $this->res['msg'] = '任务发布成功，等待管理员审核！';
	        $this->res['data'] = $res;
	        $this->response($this->res,'json');
	    }else{
	        $this->res['code'] = 500;
	        $this->res['msg'] = '操作失败，请稍后再试！';
	        $this->response($this->res,'json');
	    }
	}
	
}
    
    